package com.itheima.securitydemo.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.itheima.securitydemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Component
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //写数据库访问的代码了
        QueryWrapper<com.itheima.securitydemo.pojo.User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        com.itheima.securitydemo.pojo.User mysqlUser = userMapper.selectOne(queryWrapper);
        Collection<GrantedAuthority> authorties = new ArrayList<>();

        //查询用户对应的权限信息
        List<String> list = userMapper.selectRoles(username);
        if (list != null && list.size()>0) {
            for (String role :list) {
                System.out.println(role);
                authorties.add(new SimpleGrantedAuthority(role));
            }
        }



        if(mysqlUser == null){return null;}
        return new User(mysqlUser.getUserName(), mysqlUser.getPassword(), authorties);
    }
}
